package com.model.single;

/**
 * ThreadLocal 类实现单例模式
 *
 * @author innocent
 * @date 2019/1/17 14:47
 */
public class ThreadLocalSingleton {
    //构造方法私有化
    private ThreadLocalSingleton(){}

    //自己创建一个实例
    private static final ThreadLocal<ThreadLocalSingleton> tls = new ThreadLocal<ThreadLocalSingleton>(){
        @Override
        protected ThreadLocalSingleton initialValue() {
            return new ThreadLocalSingleton();
        }
    };

    //对外提供公共的访问方法
    public ThreadLocalSingleton getInstance() {
        return tls.get();
    }
}

